module input_box #(
    parameter 
        FRAME_LINE = 16'd640,
        FRAME_ROW  = 16'd480,
        BOX_COLOR  = 16'hf800 // 纯红
)(
    input                   clk              ,
    input                   rst_n            ,
    input                   pre_vsync        ,
    input                   pre_href         ,
    input                   pre_data_valid   ,
    input      [15:0]       pre_data         ,
    input      [15:0]       coord_x1         ,
    input      [15:0]       coord_x2         ,
    input      [15:0]       coord_y1         ,
    input      [15:0]       coord_y2         ,
    // input      [655:0]      pre_target_pos   ,        // valid row line
    output                  post_vsync       ,
    output                  post_href        ,
    output reg [15:0]       post_data        
);
// 一维数组
// wire [40:0]  target_pos [15:0];
// reg
reg [15:0]  row_cnt;
reg [15:0]  line_cnt;
reg         pre_vsync_d1;
reg         pre_href_d1;

// main code---------------------------------------------------------------------
// target_pos
// generate
//     genvar i;
//     for (i = 0;i < 16 ;i = i + 1)begin :gen_target_pos
//        assign target_pos[i] = pre_target_pos[40 + i * 41 : i * 41];
//     end
// endgenerate

// pre_vsync_d1 & pre_href_d1
always @(posedge clk or negedge rst_n) begin
    if(~rst_n)begin
        pre_vsync_d1    <= 1'd0;
        pre_href_d1     <= 1'd0;
    end
    else begin
        pre_vsync_d1 <= pre_vsync;
        pre_href_d1  <= pre_href;
    end 
end

// row_cnt
always @(posedge clk or negedge rst_n) begin
    if(~rst_n)
        row_cnt <= 16'd0;
    else if((line_cnt == FRAME_LINE) && (row_cnt == FRAME_ROW - 16'd1))
        row_cnt <= 16'd0;
    else if(line_cnt == FRAME_LINE)
        row_cnt <= row_cnt + 16'd1;
    else
        row_cnt <= row_cnt;
end

// line_cnt
always @(posedge clk or negedge rst_n) begin
    if(~rst_n)
        line_cnt <= 16'd0;
    else if(line_cnt == FRAME_LINE)
        line_cnt <= 16'd0;
    else if(pre_data_valid)
        line_cnt <= line_cnt + 16'd1;
    else
        line_cnt <= line_cnt;
end

// outputs-------------------------------------------------------------------------
// post_data
// integer j;
// always @(posedge clk or negedge rst_n) begin
//     if(~rst_n)
//         post_data <= 16'd0;
//     else begin
//         for (j = 0; j < 16; j = j + 1)begin
//             if(target_pos[j][40])begin
//                 if(((row_cnt == target_pos[j][39:30]) || (row_cnt == target_pos[j][19:10])) && (line_cnt >= target_pos[j][29:20]) && (line_cnt <= target_pos[j][ 9: 0]))
//                     post_data <= BOX_COLOR;
//                 else if(((line_cnt == target_pos[j][29:20]) || (line_cnt == target_pos[j][ 9: 0])) && (row_cnt >= target_pos[j][39:30]) && (row_cnt <= target_pos[j][19:10]))
//                     post_data <= BOX_COLOR;
//                 else
//                     post_data <= pre_data;
//             end
//             else
//                 post_data <= pre_data;
//         end
//     end
// end

always @(posedge clk or negedge rst_n) begin
    if(~rst_n)
        post_data <= 16'd0;
    else begin
        if(((row_cnt == coord_y1) || (row_cnt == coord_y2)) && (line_cnt <= coord_x2) && (line_cnt >= coord_x1))
            post_data <= BOX_COLOR;
        else if(((line_cnt == coord_x1) || (line_cnt == coord_x2)) && (row_cnt <= coord_y2) && (row_cnt >= coord_y1))
            post_data <= BOX_COLOR;
        // if((line_cnt == coord_x2) && (row_cnt <= coord_y2) && (row_cnt >= coord_y1)) // 为什么颜色很淡？ 计数的问题，要在行计数时，对像素点进行计数，而不是8bit为一个单位计数
        //     post_data <= BOX_COLOR;
        else
            post_data <= pre_data;
    end
end

// post_vsync & post_href
assign post_vsync = pre_vsync_d1;
assign post_href = pre_href_d1;
endmodule